const helper = new Helper(this);
// 基础服务
const basicRequest = BasicServices.getInstance();
// 应急服务
const emergencyRequest = EmergencyServices.getInstance();

globalThis.__drill__services = {
    /**
     * 成员
     *
     * @param eventId
     * @returns {*}
     */
    requestPerson(eventId) {
        return emergencyRequest.getRecordFilterCriteriaByEventId(eventId).then((result) => {
            console.log(this, result);
            return result?.personList?.map(item => ({
                name: item, value: item,
            }))
        })
    },

    /**
     * 字典
     *
     * @param dictclassCode
     * @returns {*}
     */
    requestDict(dictclassCode) {
        return basicRequest.getDictInfoList({ dictclassCode }).then(({ success, result }) => {
            console.log(this, result);
            return result?.rows?.map(item => ({
                ...item, name: item.dictName, value: item.dataValue,
            }))
        })
    },

    /**
     * 获取事件记录进度
     *
     * @param eventId
     * @returns {*}
     */
    getProcessRecordByEventId(eventId) {
        return emergencyRequest.getProcessRecordByEventId(eventId, '1')
    },

    /**
     * 获取事件记录
     *
     * @param eventId
     * @returns {*}
     */
    getEventRecordByEventId(eventId) {
        return emergencyRequest.getEventRecordByEventId(eventId)
    },
}

globalThis.__drill__ = {
    queryDrillDetail ()  {
        return queryManage.queryDrillDetail(params.data.id).then(res => {
            const { xCoordinate, yCoordinate, zCoordinate, startTime, endTime } = res;
            return {
                ...res,
                drillDatetimeRange: `${startTime} 至 ${endTime}`,
                occurrencePlace: `${xCoordinate} , ${yCoordinate} , ${zCoordinate}`,
            };
        });
    }
}


globalThis.__drill__list = {
    // 事件
    activeParent: null,

    /**
     * 刷新列表
     *
     * @return {*}
     */
    reloadDrillList(parent) {
        const panel = helper.get('event-panel')?.widgetObj;
        const queryManage = helper.get('queryManage')?.widgetObj;
        const type = helper.getVariable('panelType') || 0;

        // 【新增、解除、关闭时】刷新数据集数量，需要的参数
        helper.setVariable('event_type', type);

        if (+type === 0) {
            const process = helper.getVariable('whetherProcess') || 0;
            const searchText = helper.getVariable('event_search');
            return queryManage.queryDrillNews(+process, searchText).then(data => {
                panel.setData([{ data }]);
                panel.render()
                return data;
            })
        } else {
            __drill__list.activeParent = parent
            __drill__list.queryDrillList(1, parent)
        }
    },

    /**
     * 查询演练列表
     *
     * @param pageNum
     * @param parent
     * @return {*}
     */
    queryDrillList(pageNum = 1, parent) {
        const type = helper.getVariable('panelType') || 0;
        const process = helper.getVariable('whetherProcess') || 0;
        const searchText = helper.getVariable('event_search');
        const queryManage = helper.get('queryManage')?.widgetObj;
        const panel = helper.get('event-panel')?.widgetObj;
        const { title, uniqueKey, projectCode } = parent || __drill__list.activeParent;
        const params = {
            pageNum,
            pageSize: 10,
            projectCode: JSON.stringify([ projectCode ]),
            // projectCodeList: JSON.stringify([$event.projectCode]),
            eventTitle: searchText,
            drillName: searchText,
            disableFlag: process
        };

        if (+type === 1) {
            if (title) params.drillLocation = title
        } else if (+type === 2) {
            if (title) params.eventType = title
        } else if (+type === 3) {
            if (title) params.eventGrade = title
        }

        return queryManage.queryDrillList(params).then(({ success, result, count, ...other }) => {
            const { page = {} } = panel._option?.panel_config || {};
            if (success) {
                const option = {
                    dataSource: result,
                    total: count,
                    page: { ...page, current: 1 }
                };
                // panel.setData([{data: res}]);
                // panel.render();
                panel.tree.pannelRerender(uniqueKey, option);
            }
            return { success, result, count, ...other };
        })
    }
}

globalThis.__drill__record_form = {
    options: {},
}


